#!/bin/sh
#===============================================================================
#
#  standby-actions
#
#  Copyright (C) 2023, 2024 by Digi International Inc.
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2 as published by
#  the Free Software Foundation.
#
#
#  !Description: manage interfaces before suspending and after resuming from
#                suspend
#
#===============================================================================

RESUME_FILE="/tmp/resume_actions"
RESUME_ACTIONS=""

wifi_actions_needed() {
	[ -d "/proc/device-tree/wireless" ] && [ ! -e "/sys/firmware/devicetree/base/soc@0/bus@42800000/mmc@428b0000/keep-power-in-suspend" ]
}

bt_actions_needed() {
	systemctl -q is-active bluetooth-init
}

if [ "${1}" = "pre" ]; then
	rm -f "${RESUME_FILE}"

	# Stop NetworkManager before suspend
	systemctl stop NetworkManager

	if bt_actions_needed; then
		# bluetooth service relies on bluetooth-init service so
		# stop it unconditionally
		systemctl stop bluetooth-init
		systemctl stop bluetooth
		# Program the resume actions to start the services
		RESUME_ACTIONS_BT="systemctl start bluetooth-init; systemctl start bluetooth;"
	fi

	if wifi_actions_needed; then
		RESUME_ACTIONS_WIFI=""
		for iface in wlan0 uap0 wfd0; do
			if grep -qs ${iface} /var/run/ifstate; then
				# Bring the interface down
				ifdown ${iface}
				# Program the resume action to bring it up
				# (prepend to use reverse order)
				RESUME_ACTIONS_WIFI="ifup ${iface};${RESUME_ACTIONS_WIFI}"
			fi
		done

		# Unload Wi-Fi modules
		modprobe -r moal
		# Program the resume action to reload the modules
		# (prepend to use reverse order)
		RESUME_ACTIONS_WIFI="/etc/udev/scripts/load_iw612.sh;${RESUME_ACTIONS_WIFI}"
	fi

	# Compound resume actions (enable BT first, or else add a sleep, to give
	# some time to the system to be ready to load the Wi-Fi)
	if [ -n "${RESUME_ACTIONS_BT}" ]; then
		RESUME_ACTIONS="${RESUME_ACTIONS_BT}"
	fi
	if [ -n "${RESUME_ACTIONS_WIFI}" ]; then
		if [ -z "${RESUME_ACTIONS_BT}" ]; then
			RESUME_ACTIONS="sleep 0.5;"
		fi
		RESUME_ACTIONS="${RESUME_ACTIONS}${RESUME_ACTIONS_WIFI}"
	fi

	if [ -n "${RESUME_ACTIONS}" ]; then
		# Create temp file with resume actions
		echo "${RESUME_ACTIONS}" > "${RESUME_FILE}"
		chmod +x "${RESUME_FILE}"
	fi
elif [ "${1}" = "post" ]; then
	if [ -f "${RESUME_FILE}" ]; then
		eval "${RESUME_FILE}"
		# Clean-up
		rm -f "${RESUME_FILE}"
	fi

	# Resume NetworkManager after suspend
	systemctl start NetworkManager
fi
